registerEvent()

const eventMap = new Map();

function registerEvent(eventList) {
    if (eventList === undefined) {
        return
    }
    console.log("注册事件")
    for (let i = 0; i < eventList.length; i++) {
        let tempEventInfo = eventList[i]
        let eventName = tempEventInfo.event_name
        if (eventMap.get(eventName) !== undefined) {
            console.log("已经注册过了，跳过")
            continue
        }
        eventMap.set(eventName, 1)
        // 再重新绑定事件
        $("body").on("click", tempEventInfo.event_element, eventHandler(tempEventInfo));

        console.log("注册事件：" + eventName);
    }
}

function eventHandler(event) {
    // index_type 1 -指定索引 2-自己的索引 ai-index 3-全部索引
    return function () {
        let data = []
        for (let i = 0; i < event.query_list.length; i++) {
            let queryInfo = event.query_list[i]
            let tempData = {}
            if (queryInfo.index_type === 1) {
                tempData = getTempData(queryInfo, queryInfo.index)
            } else if (queryInfo.index_type === 2) {
                let selfIndex = $(this).data("ai-index")
                tempData = getTempData(queryInfo, selfIndex)
            } else {
                let htmlLength = document.querySelectorAll(queryInfo.selector).length
                tempData = {
                    name: queryInfo.name,
                    list: []
                }
                for (let i = 0; i < htmlLength; i++) {
                    let html = document.querySelectorAll(queryInfo.selector)[i].innerHTML
                    let tempItem = {index: i, value: html}
                    tempData.list.push(tempItem)
                }
            }
            data.push(tempData)
        }

        let message = {
            action: 'sendEventContent',
            event: event.id,
            agent_id: event.agent_id,
            page_data: data
        }
        chrome.runtime.sendMessage(message, function (response) {
            // console.log("Response from background script:", response);
        });
    }
}

function getTempData(queryInfo, index) {
    let html = document.querySelectorAll(queryInfo.selector)[index].innerHTML
    let tempData = {
        name: queryInfo.name,
        list: [{index: index, value: html}]
    }
    return tempData
}

// let a = $(".box-card .card-header").eq(0);
// console.log("注入成功");
// $("body").on("click", ".okr-ai", function () {
//     console.log(a)
//     let html = document.querySelectorAll(".box-card.el-card")[0]
//     console.log(html)
//
//     return
//
//     let index = $(this).data("index")
//     let responseData = getOkrInfo(index)
//
//     let msgInfo = "帮我优化这个 OKR \r\n O:" + responseData.o + "\r\n"
//     for (let i = 0; i < responseData.kr.length; i++) {
//         msgInfo += " KR" + (i + 1) + ": " + responseData.kr[i] + "\r\n"
//     }
//
//     let message = {
//         action: 'sendUserMsg',
//         data: msgInfo
//     }
//
//     chrome.runtime.sendMessage(message, function (response) {
//         console.log("Response from background script:", response);
//     });
// })

chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
    let responseData
    // if (message.action === 'getOkrInfo') {
    //     responseData = getOkrInfo(message.index)
    // }
    // if (message.action === 'getOkrList') {
    //     responseData = getOkrList()
    // }

    if (message.action === 'registerEvent') {
        registerEvent(message.data)
        responseData = {code: 0}
    }

    sendResponse({data: responseData});
    return true;
});

// function getOkrList() {
//     const okrNumber = $(".box-card .card-header .mentions-input").length
//     let okrList = []
//     for (let i = 0; i < okrNumber; i++) {
//         let tempOkr = $(".box-card .card-header .mentions-input").eq(i).text()
//         okrList.push(tempOkr)
//     }
//     return okrList
// }
//
// function getOkrInfo(index) {
//     // 这里假设要获取 ID 为 target-element 的元素的值
//     // const targetElement = document.getElementById('target-element');
//     // const elementValue = document.querySelector(".box-card .mentions-input").textContent
//
//     const o = $(".box-card .card-header .mentions-input").eq(index).text()
//     const krNumber = $(".el-card.box-card").eq(index).find(".el-card__body .okr-item-wrap").length
//     let okrInfo = {
//         o: o,
//         kr: []
//     }
//     for (let i = 0; i < krNumber; i++) {
//         let tempKr = $(".el-card.box-card").eq(index).find(".el-card__body .okr-item-wrap").eq(i).find(".mentions-input").text()
//         okrInfo.kr.push(tempKr)
//     }
//     return okrInfo
// }